package edu.upc.fib.lamsandroid.server.lams;

import java.io.File;

public class Util {

    private static final int LAMS_CR_NODE_ID_CYPHERS = 4;

    // TODO: parametrize
    private static final String CR_BASE_PATH_DEVELOPMENT = "D:\\lams2\\data\\repository";
    private static final String CR_BASE_PATH_PRODUCTION = "/var/opt/lams/repository";

    private static final String CR_BASE_PATH;
    private static final String SECURE_BASE_PATH="/usr/local/jboss-5.1/server/default/deploy/lams.ear/lams-www.war/secure";

    static {
        String osName = System.getProperty("os.name").toLowerCase();
        if(osName.indexOf("win") != -1) {
            CR_BASE_PATH = CR_BASE_PATH_DEVELOPMENT;
        } else {
            CR_BASE_PATH = CR_BASE_PATH_PRODUCTION;
        }

    }

    public static File getContentRepositoryFile(String nodeId) {
        for(int i=0 ; i<LAMS_CR_NODE_ID_CYPHERS ; i++) {
            StringBuffer buffer = new StringBuffer(CR_BASE_PATH + File.separator);

            for(int j=0 ; j<i ; j++) {
                buffer.append("0");
            }

            buffer.append(nodeId + File.separator + "1");

            File file = new File(buffer.toString());

            if(file.exists()) {
                return file;
            }
        }

        return new File(CR_BASE_PATH + File.separator + nodeId + File.separator + "1");
    }


    public static File getSecureFile(String url) {
        return new File(SECURE_BASE_PATH + url);
    }
}
